package org.dei.perla.aggregates;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import org.dei.perla.parser.expressions.Constant;
import org.dei.perla.parser.expressions.ConstantCastException;
import org.dei.perla.parser.expressions.ConstantFloat;
import org.dei.perla.parser.expressions.ConstantID;
import org.dei.perla.parser.expressions.ConstantInteger;
import org.dei.perla.parser.expressions.ConstantTimestamp;
//import org.dei.perla.sys.query.llqruntime.datastructures.Buffer;
import org.dei.perla.sys.query.llqruntime.datastructures.Buffer;
import org.dei.perla.sys.query.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;

public final class StreamVariance {
	// Record random generation
	public static Record randomRecord(
			QueryRecordStructure parQueryRecordStructure) {
		Record rec = new Record(parQueryRecordStructure);
			Constant c1 = new ConstantID("A");
			Date now = new Date();
		    long nowLong = now.getTime();
			Constant c2 = new ConstantTimestamp(nowLong);
			Constant c3 = new ConstantInteger(BigDecimal.valueOf(
					Math.random() * 10).intValue());
			rec.setField("ID", c1);
			rec.setField("TS", c2);
			rec.setField("TEMP", c3);
		return rec;
	}
	
	public static ConstantFloat[] sampleArray(Buffer pBuffer,
			String fieldName) throws ConstantCastException {
		ArrayList<Record> list = pBuffer.getBuffer();
		Iterator<Record> it = list.iterator();
		ConstantFloat[] sampleData = new ConstantFloat[list.size()];
		double data = 0;//new double[list.size()];
		int i = 0;
//		Class<?> type = null;
		while (it.hasNext()) {
			Record t = it.next();
//			type = t.getRecordStructure().get(fieldName).getFieldType();
			data = t.getField(fieldName).getValueFloat();
			sampleData[i] = new ConstantFloat(data);
			System.err.println("ID: " + t.getField("ID") + " - TS: "
					+ t.getField("TS") + " - TEMP : " + t.getField("TEMP"));
			i++;
		}
		return sampleData;
	}
}
